import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:osc/res/colors.dart';
import 'package:osc/res/dimens.dart';
import 'package:osc/ui/widgets/real_rich_text.dart';
import 'package:osc/utils/pd_util.dart';
import 'package:osc/utils/string_util.dart';

import 'image_loader.dart';

class SmallWidgets  {

  static buildLine({Color color = Colours.divider, double height = Dimens.gap_divider_height_smallest}){
    return Container(color: color, height: height);
  }
  static buildLineBigger({Color color = Colours.divider_big, double height = Dimens.gap_divider_height_big}){
    return Container(color: color, height: height);
  }

  static buildArrowRight(){
    double size = Dimens.view_item_icon_height_smallest;
    return Padding(
      padding: EdgeInsets.only(left: Dimens.gap_view_normal),
      child: Image.asset("images/icon_arrow_right.png", width: size, fit: BoxFit.cover,),
    );
  }
  static buildArrowDown(){
    double size = Dimens.view_item_icon_height_smallest;
    return Padding(
      padding: EdgeInsets.only(left: Dimens.gap_view_normal),
      child: Image.asset("images/icon_arrow_down_gray.png", height: size, fit: BoxFit.cover,),
    );
  }

  static buildText(String data, Color color, double fontSize, {TextAlign textAlign = TextAlign.left, FontWeight fontWeight = FontWeight.normal, TextDecoration textDecoration = TextDecoration.none, double height = 1, bool softWrap = true, TextOverflow overflow, int maxLines}){
    return Text(data, style: TextStyle(color: color, fontSize: fontSize, fontWeight: fontWeight, decoration: textDecoration, height: height), textAlign: textAlign, softWrap: softWrap, overflow: overflow, maxLines: maxLines,);
  }

  static buildTextWithStyle({@required String data, @required TextStyle textStyle, TextAlign textAlign = TextAlign.left, bool softWrap = true}){
    return Text(data, style: textStyle, textAlign: textAlign, softWrap: softWrap,);
  }

  static buildImage({@required String imageName, double size = Dimens.view_item_icon_height_normal, BoxFit fit = BoxFit.contain}){
    return Image.asset(PdUtil.getImagePath(imageName), width: size, fit: fit,);
  }

  static buildWidgetBgColor({@required Color color, @required Widget child, EdgeInsetsGeometry margin = EdgeInsets.zero,
    EdgeInsetsGeometry padding = EdgeInsets.zero, AlignmentGeometry alignment = Alignment.center}){
    return Container(
      color: color,
      alignment: alignment,
      margin: margin,
      padding: padding,
      child: child,
    );
  }

  static buildWidgetBgImage({@required String imageName, @required Widget child, EdgeInsetsGeometry margin = EdgeInsets.zero,
    EdgeInsetsGeometry padding = EdgeInsets.zero, BoxFit fit = BoxFit.cover, AlignmentGeometry alignment = Alignment.center}){
    return Container(
      margin: margin,
      padding: padding,
      height: Dimens.view_item_height_normal,
      alignment: alignment,
      decoration: BoxDecoration(
        image: DecorationImage(
          image: AssetImage(PdUtil.getImagePath(imageName)),
          fit: fit,
        ),
      ),
      child: child,
    );
  }

  static buildWidgetBgBorder({@required Color color, @required Color borderColor, @required Widget child, EdgeInsetsGeometry margin = EdgeInsets.zero,
    EdgeInsetsGeometry padding = EdgeInsets.zero, BoxFit fit = BoxFit.cover, AlignmentGeometry alignment = Alignment.center, double radius = 5}){
    return Container(
      margin: margin,
      padding: padding,
      height: Dimens.view_item_height_normal,
      alignment: alignment,
      decoration: BoxDecoration(
        color: color,
        border: Border.all(color: borderColor, width: 0.5),
        borderRadius: BorderRadius.circular(radius)
      ),
      child: child,
    );
  }

  static buildAction({@required String data, @required Function onTap, Color textColor = Colors.black54}){
    return Center(
      child: Ink(
        color: Colours.app_main,
        child: InkWell(
          onTap: onTap,
          child: Container(
            padding: EdgeInsets.symmetric(horizontal: Dimens.gap_view_bigger, vertical: Dimens.gap_view_big),
            child: buildText(data, textColor, Dimens.font_action),
          ),
        ),
      ),
    );
  }

  static buildActionIcon({String data, String imageName, Function onTap, Color bgColor = Colours.app_main, Color textColor = Colors.black54}){
    return Center(
      child: Ink(
        color: bgColor,
        child: InkWell(
          onTap: onTap,
          child: Container(
            padding: EdgeInsets.symmetric(horizontal: Dimens.gap_view_bigger, vertical: Dimens.gap_view_normal),
            child: StringUtil.isEmpty(data)?Image.asset(PdUtil.getImagePath(imageName)):buildText(data, textColor, Dimens.font_action),
          ),
        ),
      ),
    );
  }

  static buildActionIconButton({@required String imageName, @required BuildContext context, Function onTap}){
    return IconButton(
        icon: Image.asset(PdUtil.getImagePath(imageName), width: IconTheme.of(context).size, fit: BoxFit.fitWidth),
        onPressed: onTap
    );
  }

  static buildImageSpan({@required String imageName, EdgeInsets margin = EdgeInsets.zero, double size = Dimens.view_item_tag_height_small}){
    return ImageSpan(
      AssetImage(PdUtil.getImagePath(imageName)),
      imageWidth: size,
      imageHeight: size,
      margin: margin,
    );
  }

  static buildCircleImage({@required String url, String errorImageName = "ic_split_graph", String placeholderImageName, BoxFit fit = BoxFit.cover, double width, double height}){
    return SizedBox(
      width: width,
      height: height,
      child: ClipOval(
        child: ImageLoader(url: url, errorImageName: errorImageName, placeholderImageName: placeholderImageName, fit: fit, width: width, height: height,),
      ),
    );
  }
}
